package com.sinya.shoppingmall.fragment;

import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sinya.shoppingmall.R;
import com.sinya.shoppingmall.widget.SearchView;

/**
 * 搜索
 *
 * @author KoizumiSinya
 * @date 2015/12/25. 0:12
 */

public class SearchFragment extends BaseFragment {

    private TextView tv_title;
    private SearchView searchView;

    // [+] Handler

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:

                    break;
                default:
                    break;
            }

            return false;
        }
    });

    // [-] Handler
    // [+] Override BaseFragment Method

    @Override
    protected View initView() {
        return View.inflate(mContext, R.layout.fragment_three, null);
    }

    @Override
    protected void setView() {
        initSearchView();
        initSearchDataView();
    }

    @Override
    protected void setTitle() {
        tv_title = (TextView) rootView.findViewById(R.id.tv_title);
        tv_title.setText(mContext.getString(R.string.title_search));
    }

    @Override
    public void onResume() {
        searchView.initDefalutSearchView();
        super.onResume();
    }

    // [-] Override BaseFragment Method

    private void initSearchView() {
        searchView = (SearchView) rootView.findViewById(R.id.search);
        searchView.setSearchListener(new SearchView.onSearchInputListener() {
            @Override
            public void editTextChanged() {

            }

            @Override
            public void onCancel() {
                initSearchDataView();
            }

            @Override
            public void onSearch() {
                //点击搜索 将输入的关键字文本
                if (!TextUtils.isEmpty(searchView.getInputText())) {
                    SearchResultFragment fragment = SearchResultFragment.getInstanse();
                    fragment.executeSearch(searchView.getInputText());
                    replaceFrameLayout(fragment);
                }
            }

            @Override
            public void onClickSearch() {

            }
        });
    }

    private void initSearchDataView() {
        SearchHistoryFragment searchFragment = SearchHistoryFragment.getInstance();
        searchFragment.getData();
        replaceFrameLayout(searchFragment);
    }

    private void replaceFrameLayout(Fragment fragment) {
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        //ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_in_left);
        ft.replace(R.id.fl_search, fragment);
        ft.commit();
    }
}
